var app = getApp();
Page({
  data: {
    shareInfo: {},
  },
  onLoad: function (e) {
    var that = this;
    wx.hideTabBar({
      animation: true
    })
    if (e && e.id) {
      that.setData({
        id: e.id,
        type: e.type
      })
    }
    if (e && e.laid) {
      that.setData({
        laid: e.laid
      })
    }
    if (e && e.enterfrom) {
      var enterfrom = e.enterfrom == 'share' ? true : false;
      that.setData({
        enterfrom: enterfrom
      })
    }
    var res = wx.getSystemInfoSync();
    that.setData({
      winW: res.windowWidth,
      winH: res.windowHeight,
      canvasW: res.windowWidth
    })
    that.requestShareinfo();

  },
  requestShareinfo: function () {
    var that = this;
    //发起请求，获取列表列表
    var id = that.data.id;
    var type = that.data.type;
    var data = {
      map: 'applet_goods_details_poster',
      gid: id,
      type: type //普通商品good，秒杀商品seckill，拼团商品group
    }
    console.log(data);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var shareInfo = res.data.data;
          that.setData({
            shareInfo: shareInfo,
            shareImg: shareInfo.cover,
            shareDesc: shareInfo.shareDesc
          })
          console.log(shareInfo);
          that.downImage(shareInfo);
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        // wx.hideLoading();
      }
    });
  },
  downImage: function (shareInfo) {
    var that = this;
    wx.downloadFile({
      url: shareInfo.cover,
      success: function (res1) {
        if (res1.statusCode == 200) {
          console.log(res1);
          shareInfo.cover = res1.tempFilePath;
          wx.downloadFile({
            url: shareInfo.qrcode,
            success: function (res2) {
              if (res2.statusCode == 200) {
                console.log(res2.tempFilePath);
                shareInfo.qrcode = res2.tempFilePath;
                wx.downloadFile({
                  url: shareInfo.shopLogo,
                  success: function (res3) {
                    console.log(res3);
                    if (res3.statusCode == 200) {
                      console.log(res3.tempFilePath);
                      shareInfo.shopLogo = res3.tempFilePath;
                      shareInfo.price = "￥" + shareInfo.price + " 扫码购买";
                      that.setData({
                        shareInfo: shareInfo
                      });
                      that.createCanvas();
                    }else{
                      that.setData({
                        drawFinish:false,
                        tips:"店铺Logo不存在"
                      })
                      wx.hideLoading();
                    }
                  },
                  fail: function () {
                    console.log("第三张图片下载失败");
                    that.setData({
                      drawFinish: false,
                      tips: "店铺logo下载失败"
                    })
                    wx.hideLoading();
                  }
                })
              }else{
                that.setData({
                  drawFinish: false,
                  tips: "商品小程序码不存在"
                })
                wx.hideLoading();
              }
            },
            fail: function () {
              console.log("第二张图片下载失败");
              that.setData({
                drawFinish: false,
                tips: "商品小程序码下载失败"
              })
              wx.hideLoading();
            }
          })
        }else{
          that.setData({
            drawFinish: false,
            tips: "商品封面不存在"
          })
          wx.hideLoading();
        }
      },
      fail: function () {
        console.log("第一张图片下载失败");
        that.setData({
          drawFinish: false,
          tips: "商品封面下载失败"
        })
        wx.hideLoading();
      }
    })
  },
  createCanvas: function () {
    var that = this;
    var shareInfo = that.data.shareInfo;
    console.log(shareInfo);
    const ctx = wx.createCanvasContext('myCanvas');
    var winW = that.data.winW;
    var winH = that.data.winH;
    var shareDesc = shareInfo.shareDesc;
    // var shareDesc = '测试测绘费收费接口事发后就开始放寒假考试过后就考试过后会计师供货商进口国还是个';
    var shareDescArr = [];
    var singleTxtnum = Math.floor((winW - 20) / 14);
    console.log(singleTxtnum);
    var txtLinenum = Math.ceil(shareDesc.length / singleTxtnum) <= 2 ? Math.ceil(shareDesc.length / singleTxtnum) : 2;
    for (var i = 0; i < txtLinenum; i++) {
      var length = shareDescArr.length;
      var txtObj = shareDesc.substring(length * singleTxtnum, length * singleTxtnum + singleTxtnum);
      shareDescArr.push(txtObj);
    }
    shareInfo.shareDesc = shareDescArr;
    var canvasW = winW;
    var canvasH = winW * 1.55 + shareDescArr.length * 14 * 1.3 + 30 + 20;
    that.setData({
      canvasW: canvasW,
      canvasH: canvasH
    })

    // console.log(shareDescArr); 
    var imgW = canvasW;
    var imgH = canvasW;
    ctx.rect(0, 0, canvasW, canvasH)
    ctx.setFillStyle('#ffffff')
    ctx.fill()
    ctx.drawImage(shareInfo.cover, 0, 0, imgW, imgH)
    var x = 10, y = imgH - 35, radius = 5, width = 70, height = 70;
    ctx.save()
    ctx.beginPath()
    ctx.moveTo(x + radius, y);
    ctx.lineTo(x + width - radius, y);
    ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
    ctx.lineTo(x + width, y + height - radius);
    ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
    ctx.lineTo(x + radius, y + height);
    ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
    ctx.lineTo(x, y + radius);
    ctx.quadraticCurveTo(x, y, x + radius, y);
    ctx.closePath();
    ctx.clip()
    var shoplogoW = canvasW * 0.2, shoplogoH = canvasW * 0.2;
    ctx.drawImage(shareInfo.shopLogo, 10, imgH - shoplogoH / 2, shoplogoW, shoplogoH)
    ctx.restore()
    ctx.setFontSize(16)
    ctx.setFillStyle('#333333')
    ctx.setTextAlign('left')
    ctx.fillText(shareInfo.shopName, shoplogoW + 20, imgH + 23);
    ctx.setFontSize(14)
    for (var i = 0; i < shareDescArr.length; i++) {
      var textY = (imgH + shoplogoW / 2 + 20) + i * 14 * 1.3;
      ctx.setFillStyle('#999999');
      ctx.fillText(shareDescArr[i], 10, textY);
    }
    var rectY = parseFloat(imgH + shoplogoW / 2 + 20 + shareDescArr.length * 14 * 1.3);
    console.log(rectY);
    ctx.setFillStyle('#ff0000')
    // ctx.setLineWidth(0.5)
    // ctx.setStrokeStyle('#333333')
    // ctx.setShadow(0, 0, 8, '#aaaaaa')
    // ctx.strokeRect(10, rectY, 140, 30)
    // ctx.setShadow(0, 0, 0, '#ffffff')
    ctx.setFontSize(16)
    ctx.fillText(shareInfo.price, 10, rectY + 10);
    ctx.drawImage(shareInfo.qrcode, canvasW * 0.65 / 2, rectY + 25, canvasW * 0.35, canvasW * 0.35)
    ctx.setTextAlign('center')
    ctx.setFontSize(13)
    ctx.setFillStyle('#999999')
    ctx.fillText("长按或扫码，立即购买", canvasW / 2, rectY + 45 + canvasW * 0.35);
    ctx.draw();
    that.setData({
      drawFinish: true
    })
    wx.hideLoading();
  },
  saveImage: function () {
    var that = this;
    var canvasW = that.data.canvasW;
    var canvasH = that.data.canvasH;
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: canvasW,
      height: canvasH,
      destWidth: canvasW * 2,
      destHeight: canvasH * 2,
      canvasId: 'myCanvas',
      fileType: 'jpg',
      success: function (res) {
        console.log(res.tempFilePath)
        var tempFilePathShow = res.tempFilePath;
        wx.showLoading({
          title: '正在保存',
          mask: true,
          time: 100000
        })
        wx.saveImageToPhotosAlbum({
          filePath: tempFilePathShow,
          success(res) {
            console.log(res);
            app.errorTip(that, "图片保存成功", 2000);
          },
          fail(f) {
            app.errorTip(that, "图片保存失败", 2000);
          },
          complete() {
            wx.hideLoading();
          }
        })
      }
    })
  },
  onShareAppMessage: function () {
    var that = this;
    var id = that.data.id;
    var type = that.data.type;   
    var url = '';
    if (type == 'good') {
      url = '/subpages1/goodDetail/goodDetail?enterfrom=share&id=' + id;
    } else if (type == 'seckill') {
      var laid = that.data.laid;
      url = '/subpages1/goodDetail/goodDetail?enterfrom=share&id=' + id + '&laid=' + laid;
    } else if (type == 'group') {
      url = '/subpages1/groupGoodDetail/groupGoodDetail?enterfrom=share&goodid=' + id;
    }
    var cover = that.data.shareImg;
    var title = that.data.shareDesc;
    app.getPoint(that);
    return {
      title: title,
      imageUrl: cover,
      path: url
    }
  }
})